package template.web;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 * web常用函数
 * 
 * @author megnyang
 *
 */
public class RequestUtils {
	
	public static final Logger LOGGER = Logger.getLogger(RequestUtils.class);

	/**
	 * 获取指定名字的参数值
	 * 若参数值为空则返回null
	 * @param paramName 参数名
	 * @param request
	 * @return
	 */
	public static String getString(String paramName, HttpServletRequest request) {
		String temp = request.getParameter(paramName);
		if (StringUtils.isNotEmpty(temp)) {
			return temp;
		} else {
			return null;
		}
	}

	/**
	 * 获取指定名字的参数值
	 * 若参数值为空则返回默认值
	 * @param paramName 参数名
	 * @param defaultString
	 * @param request 默认值
	 * @return
	 */
	public static String getString(String paramName, String defaultString,
			HttpServletRequest request) {
		String temp = getString(paramName, request);
		if (StringUtils.isEmpty(temp)) {
			temp = defaultString;
		}
		return temp;
	}

	/**
	 * 获取指定名字的参数值，并转化为整形数字
	 * @param paramName
	 * @param request
	 * @return
	 * @throws NumberFormatException
	 */
	public static int getInt(String paramName, HttpServletRequest request)
			throws NumberFormatException {
		return Integer.parseInt(getString(paramName, request));
	}

	/**
	 * 获取指定名字的参数值，并转化为整形数字
	 * 若参数值为空或转化失败则返回默认值
	 * @param paramName
	 * @param defaultInt
	 * @param request
	 * @return
	 */
	public static int getInt(String paramName, int defaultInt,
			HttpServletRequest request) {
		try {
			String temp = getString(paramName, request);
			if (StringUtils.isNotBlank(temp)) {
				return Integer.parseInt(temp);
			}
		} catch (NumberFormatException e) {
			LOGGER.error("转化字符串为数字出错", e);
		}
		return defaultInt;
	}

	/**
	 * 获取指定名字的参数值，并转化为长整形数字
	 * @param paramName
	 * @param request
	 * @return
	 * @throws NumberFormatException
	 */
	public static long getLong(String paramName, HttpServletRequest request)
			throws NumberFormatException {
		return Long.parseLong(getString(paramName, request));
	}

	/**
	 * 获取指定名字的参数值，并转化为长整形数字
	 * 若参数值为空或转化失败则返回默认值
	 * @param paramName
	 * @param defaultInt
	 * @param request
	 * @return
	 */
	public static long getLong(String paramName, long defaultLong,
			HttpServletRequest request) {
		try {
			String temp = getString(paramName, request);
			if (StringUtils.isNotBlank(temp)) {
				return Long.parseLong(temp);
			}
		} catch (NumberFormatException e) {
			LOGGER.error("转化字符串为数字出错", e);
		}
		return defaultLong;
	}
	
	/**
	 * 返回指定名字的整形数组
	 * @param paramName
	 * @param request
	 * @return
	 * @throws NumberFormatException
	 */
	public static Integer[] getIntArray(String paramName, HttpServletRequest request)throws NumberFormatException {
		Integer[] intArray = null;

		String[] tempArray = request.getParameterValues(paramName);
		if(tempArray != null && tempArray.length > 0){
			int lengh = tempArray.length;
			intArray = new Integer[lengh];
			for(int i = 0; i < lengh; i ++){
				intArray[i] = Integer.valueOf(tempArray[i]);
			}
		}
		return intArray;
	}
	
	public static Integer[] getIntArrayBySplit(String paramName, String split, HttpServletRequest request){
		Integer[] intArray = null;
		
		String temp = getString(paramName, request);
		if(StringUtils.isNotBlank(temp)){
			String[] tempArray = temp.split(split);
			if(tempArray != null && tempArray.length > 0){
				int lengh = tempArray.length;
				intArray = new Integer[lengh];
				for(int i = 0; i < lengh; i ++){
					intArray[i] = Integer.valueOf(tempArray[i]);
				}
			}
		}
		
		return intArray;
	}
	
	/**
	 * 返回指定名字的长整形数组
	 * @param paramName
	 * @param request
	 * @return
	 * @throws NumberFormatException
	 */
	public static Long[] getLongArray(String paramName, HttpServletRequest request)throws NumberFormatException {
		Long[] longArray = null;
		String[] tempArray = request.getParameterValues(paramName);
		if(tempArray != null && tempArray.length > 0){
			int lengh = tempArray.length;
			longArray = new Long[lengh];
			for(int i = 0; i < lengh; i ++){
				longArray[i] = Long.valueOf(tempArray[i]);
			}
		}
		return longArray;
	}
	
	public static Long[] getLongArrayBySplit(String paramName, String split, HttpServletRequest request){
		Long[] longArray = null;
		
		String temp = getString(paramName, request);
		if(StringUtils.isNotBlank(temp)){
			String[] tempArray = temp.split(split);
			if(tempArray != null && tempArray.length > 0){
				int lengh = tempArray.length;
				longArray = new Long[lengh];
				for(int i = 0; i < lengh; i ++){
					longArray[i] = Long.valueOf(tempArray[i]);
				}
			}
		}
		
		return longArray;
	}
	

	/*
	 * 在评论的时候为了防止用户提交带有恶意的脚本，可以先过滤HTML标签，过滤掉双引号，单引号，符号&，符号<，符号 @
	 */
	public static String htmlEncode(String str) {
		String temp = "";
		if (str == null && !"".equals(str))
			return "";
		temp = str.replace("<", "&lt;").replace(">", "&gt;");
		return temp;
	}

	public static String htmlDecode(String str) {
		String temp = "";
		if (str == null && !"".equals(str))
			return "";
		temp = str.replace("&lt;", "<").replace("&gt;", ">");
		return temp;
	}
}
